package com.gitee.shigen.logbackspringboot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.shigen.logbackspringboot.domain.User;
import com.gitee.shigen.logbackspringboot.service.UserService;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shigenfu
 * @date 2023/12/9 19:28
 */
@RestController
@Slf4j
@Component
@RequestMapping(value = "test")
public class TestController {

    @Resource
    private UserService userService;

    @GetMapping(value = "date")
    public String test() {
        String date = DateUtil.now();
        log.info("current Date: {}", date);
        return date;
    }

    @Scheduled(cron = "0/5 * * * * *")
    public void task() {
        log.info("current seconds : {}", DateUtil.currentSeconds());
    }

    @GetMapping(value = "user/{id}")
    public User getById(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    @GetMapping(value = "user/page/{pagenum}/{pagesize}")
    public IPage<User> selectPage(@PathVariable(value = "pagenum", required = false) Integer pagenum,
        @PathVariable(value = "pagesize", required = false) Integer pagesize) {
        if (pagenum == null) {
            pagenum = 1;
        }
        if (pagesize == null) {
            pagesize = 10;
        }
        Page<User> userPage = new Page<>(pagenum, pagesize);
        return userService.page(userPage, null);
    }

    @GetMapping(value = "info")
    public String getInfo() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        String hostName = localHost.getHostName();
        String hostAddress = localHost.getHostAddress();
        return hostName + " " + hostAddress;
    }

}
